package edu.tongji.structureplayer.file
{
    import flash.filesystem.File;
    import flash.filesystem.FileMode;
    import flash.filesystem.FileStream;
    
    import mx.controls.Alert;
    
    public class StructureFileManager implements StructureFileManagerI
    {
        
        public function save(structure:String, file:File):void
        {
            if (null == file)
                return;
            
            if (!file.exists)
            {
                var stream:FileStream = new FileStream();
                stream.open(file, FileMode.WRITE);
                stream.writeUTFBytes(structure);
                stream.close();
            } 
            else 
            {
                Alert.show("File " + file.name + " exists!");
            }
        }
        
        public function open(file:File):String
        {
            if (null == file)
                return null;
            
            if (file.exists)
            {
                var stream:FileStream = new FileStream();
                stream.open(file, FileMode.READ);
                var structure:String = stream.readUTFBytes(stream.bytesAvailable);
                stream.close();
                return structure;
            }
            else
            {
                Alert.show("File " + file.name + " does not exists!");
                return null;
            }
        }
    }
}